#include "lvgl_app/lvgl_app.h"

 /*枚举
 界面ID
     PM_ID_SETTING_MENU

 界面参数
     无
 */


 /* 语言切换 start */
static const char* exp_lab_str_list_en[] =
{
    "System settings"
};
static const char* exp_lab_str_list_zh[] =
{
    "系统设置"
};
static char** exp_lab_str_list = (char**)exp_lab_str_list_en;

static const char* btn_lab_str_list_en[] =
{ "Date Setting","Backlight Setting","Language","Info"};
static const char* btn_lab_str_list_zh[] =
{ "时间设置","背光设置","语言","关于" };
static char** btn_lab_str_list = (char**)btn_lab_str_list_zh;
static const uint8_t btn_lab_str_list_num = 4;
/* 语言切换 end */

/* 回调配置 start */
static void btn_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* obj = lv_event_get_target(e);

    if (code == LV_EVENT_SHORT_CLICKED)
    {
        /* 通过传入的user data得到当前按键的index */
        uint32_t index = ((size_t)(e->user_data) - (size_t)(&btn_lab_str_list[0])) / sizeof(size_t);
        lv_pm_printf("index -> %d \r\n", index);
        switch (index)
        {
        case 0:
        {
            lv_pm_control.open(PM_ID_SETTING_TIME, &options_default);
        }break;
        default:
            break;
        }
    }
}

static void backlight_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* obj = lv_event_get_target(e);
    if (code == LV_EVENT_VALUE_CHANGED) {
        char buf[32];
        uint16_t time = 0;
        lv_dropdown_get_selected_str(obj, buf, sizeof(buf));

        LV_LOG_USER("Option: %s %d", buf, lv_dropdown_get_option_index(obj, buf));

        switch( lv_dropdown_get_option_index(obj, buf) )
        {
            case 0:
                time = 5;
            break;
            case 1:
                time = 15;
            break;
            case 2:
                time = 30;
            break;
            case 3:
                time = 1*60;
            break;
            case 4:
                time = 5*60;
            break;
            case 5:
                time = 10*60;
            break;
            case 6:
                time = 30*60;
            break;
            case 7:
                time = 0;
            break;
        }
        OS_SetBankDownTime(time);
    }
}
static void language_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* obj = lv_event_get_target(e);
    if (code == LV_EVENT_VALUE_CHANGED) {
        char buf[32];
        lv_dropdown_get_selected_str(obj, buf, sizeof(buf));
        LV_LOG_USER("Option: %s", buf);
        //if (lv_dropdown_get_option_index(obj, buf) != 0)
        //{
        //    lv_pm_user_zh = 1;
        //}
        //else
        //{
        //    lv_pm_user_zh = 0;
        //}
        //LV_LOG_USER("Option: %d", lv_pm_user_zh);
    }
}
/* 回调配置 end */

static void page_onLoad(void* pm_page)
{
    lv_obj_t* page = lv_pm_get_pm_page(pm_page)->page;

    lv_obj_t* title_label = lv_label_create(page);
    lv_label_set_text(title_label, exp_lab_str_list[0]);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 5);

    lv_obj_t* cont_col = lv_obj_create(page);
    lv_obj_set_size(cont_col, LV_PCT(90), LV_PCT(65));
    lv_obj_align_to(cont_col, title_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
    lv_obj_set_style_bg_opa(cont_col, 0, LV_PART_MAIN);/* 设置页面颜色 */
    //lv_obj_set_style_width(cont_col, 0, LV_PART_SCROLLBAR);     /*滚动条宽度为0 */
    //lv_obj_set_style_bg_opa(cont_col, LV_OPA_0, LV_PART_SCROLLBAR | LV_STATE_DEFAULT);/*滚动条默认隐藏 */
    lv_obj_set_style_border_width(cont_col, 0, LV_PART_MAIN);   /*外边框宽度为0 */
    lv_obj_set_flex_flow(cont_col, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(cont_col, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_row(cont_col, 5, LV_PART_MAIN);
    lv_obj_set_style_radius(cont_col, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_all(cont_col, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_top(cont_col, 5, LV_PART_MAIN);      //图标与顶部的间隙
    lv_obj_set_scroll_snap_y(cont_col, LV_SCROLL_SNAP_START);
    
    for (uint32_t i = 0; i < btn_lab_str_list_num; i++)
    {

        lv_obj_t* btn_class = lv_btn_create(cont_col);
        lv_obj_set_style_bg_color(btn_class, lv_color_make(36, 38, 37), LV_PART_MAIN);/* 设置选择栏颜色 */
        lv_obj_set_size(btn_class, LV_PCT(95), 30);
        lv_obj_add_event_cb(btn_class, btn_event_handler, LV_EVENT_SHORT_CLICKED, &btn_lab_str_list[i]);

        lv_obj_t* btn_lab = lv_label_create(btn_class);
        lv_label_set_text(btn_lab, btn_lab_str_list[i]);
        lv_obj_align(btn_lab, LV_ALIGN_LEFT_MID, 0, 0);

        switch (i)
        {
        case 1:
        {
            lv_obj_t* dd = lv_dropdown_create(btn_class);
            lv_obj_set_size(dd, lv_pct(60), 28);
            lv_dropdown_set_dir(dd, LV_DIR_BOTTOM);
            lv_obj_set_style_text_font(dd, &lv_font_montserrat_14, 0);
            lv_dropdown_set_options(dd,
                "5s\n"
                "15s\n"
                "30s\n"
                "1min\n"
                "5min\n"
                "10min\n"
                "30min\n"
                "forever");
            lv_obj_align(dd, LV_ALIGN_RIGHT_MID, 0, 0);
            lv_obj_add_event_cb(dd, backlight_event_handler, LV_EVENT_ALL, NULL);
        }
        break;
        case 2:
        {
            lv_obj_t* dd = lv_dropdown_create(btn_class);
            lv_obj_set_size(dd, lv_pct(60), 28);
            lv_dropdown_set_dir(dd, LV_DIR_BOTTOM);
            lv_obj_set_style_text_font(dd, &lv_font_montserrat_14, 0);
            lv_dropdown_set_options(dd,
                "English\n"
                "Chinese");
            lv_obj_align(dd, LV_ALIGN_RIGHT_MID, 0, 0);
            lv_obj_add_event_cb(dd, language_event_handler, LV_EVENT_ALL, NULL);
            lv_dropdown_set_selected(dd, lv_pm_user_zh);
        }
        break;
        default:
            break;
        }
    }
    lv_pm_create.focus_obj(pm_page, lv_obj_get_child(cont_col, 0));
}

static void page_unLoad(void* pm_page)
{
    lv_obj_t* page = lv_pm_get_pm_page(pm_page)->page;
}

void page_create_page_setting_menu(lv_pm_id_t id)
{
    lv_pm_page_t* page = lv_pm_create.create(id, page_onLoad, page_unLoad);
    lv_pm_create.status_bar(page, false);
    lv_pm_create.back_bar(page, true);
}

